﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApp
{
    /// <summary>
    /// 文件读写操作
    /// </summary>
    public class FileUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="fileFullName"></param>
        public void WriteFile(string content, string fileFullName)
        {
            FileStream aFileStream = new FileStream(fileFullName, FileMode.Create, FileAccess.ReadWrite);
            StreamWriter aStreamWriter = new StreamWriter(aFileStream, Encoding.Default);
            try {
                aStreamWriter.WriteLine(content);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally{
                if (aStreamWriter != null)
                    aStreamWriter.Close();
            }
        }



        private static void Test1()
        {
            FileStream aFileStream = new FileStream("D:\\HugeData.csv", FileMode.Create, FileAccess.ReadWrite);
            FileStream bFileStream = new FileStream(@"D:\dest.txt", FileMode.Create, FileAccess.ReadWrite);
            StreamWriter aStreamWriter = new StreamWriter(aFileStream, Encoding.Default);
            StreamWriter bStreamWriter = new StreamWriter(bFileStream, Encoding.Default);
            try
            {
                for (int i = 0; i < 100000000; i++)
                {
                    aStreamWriter.WriteLine("12345678901234567890123456789012345678901234567890");
                    //this.label1.Text = i.ToString();
                }
                //MessageBox.Show("Completed!");
            }
            catch (Exception exp)
            {
                //MessageBox.Show(exp.ToString() + exp.StackTrace);
                Console.WriteLine(exp.ToString() + exp.StackTrace);
                bStreamWriter.WriteLine(exp.ToString() + exp.StackTrace);
            }
            finally
            {
                if (aStreamWriter != null)
                    aStreamWriter.Close();
                if (aFileStream != null)
                    aFileStream.Close();
            }
        }

        private static void Test2()
        {
            string sFileSrc = @"D:\\HugeData.csv";
            string sFiledst = @"C:\dest.txt";
            StreamReader sr = new System.IO.StreamReader(sFileSrc, Encoding.Default);
            StreamWriter sw = new System.IO.StreamWriter(sFiledst, false, Encoding.Default);
            int rows = 0;
            string sRead = null;
            while ((sRead = sr.ReadLine()) != null)
            {
                for (int i = 0; i < 100000; i++)
                {
                    rows++;
                    sw.WriteLine(rows.ToString() + "|" + sRead);
                    Console.WriteLine(rows.ToString() + "|" + sRead);
                }

            }
            sr.Close();
            sw.Close();
        }

        public static void Test()
        { 
        }
    }
}
